package com.learn.springcloud.hystrixexplainindetailconsumer.exception.handle.controller;

import com.learn.springcloud.hystrixexplainindetailconsumer.exception.handle.service.UserExceptionCatchAnnotationWay;
import com.learn.springcloud.hystrixexplainindetailconsumer.exception.handle.service.UserExceptionCatchExtendsWay;
import com.learn.springcloud.hystrixexplainindetailconsumer.exception.handle.service.UserServiceException;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Time : 2018/8/3 16:17
 * @Version : 1.0
 */
@RestController
public class ExceptionConsumer {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserServiceException userServiceException;

    @Autowired
    private UserExceptionCatchAnnotationWay userExceptionCatchAnnotationWay;

    /**
     * 没有异常传播
     * @param name
     * @return
     */
    @RequestMapping("/consumer/exception/handle/exception/spread/no/ignore/exception/{name}")
    public String exceptionSpread1(@PathVariable String name){
        return userServiceException.sayHelloNoIgnoreException(name);
    }

    /**
     * 带有异常传播
     * @param name
     * @return
     */
    @RequestMapping("/consumer/exception/handle/exception/spread/with/ignore/exception/{name}")
    public String exceptionSpread2(@PathVariable String name){
        return userServiceException.sayHelloWithIgnoreException(name);
    }

    /**
     * 异常捕获--继承方式
     * @param name
     * @return
     */
    @RequestMapping("/consumer/exception/handle/exception/catch/extends/way/{name}")
    public String exceptionCatch1(@PathVariable String name){
        UserExceptionCatchExtendsWay userExceptionCatchExtendsWay = new UserExceptionCatchExtendsWay(
                HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),
                restTemplate,
                name);
        return userExceptionCatchExtendsWay.execute();
    }

    /**
     * 异常捕获--注解方式
     * @param name
     * @return
     */
    @RequestMapping("/consumer/exception/handle/exception/catch/annotation/way/{name}")
    public String exceptionCatch2(@PathVariable String name) throws Exception {
        return userExceptionCatchAnnotationWay.sayHello(name);
    }

}
